    document.addEventListener('DOMContentLoaded', () => {
    // 获取DOM元素
    const modal = document.getElementById('prizeModal');
    const prizeName = document.querySelector('.prize-name');
    const closeModalBtn = document.querySelector('.close-modal');
    const remainingChancesElement = document.getElementById('remainingChances');
    const resetGameButton = document.getElementById('resetGame');
    const mysteryBoxesContainer = document.querySelector('.mystery-boxes');
    
    // 游戏状态
    let gameState = {
        remainingChances: 3,
        openedBoxes: [],
        prizes: [],
        boxes: []
    };
    
    // 初始化奖品数据
    function initPrizes() {
        gameState.prizes = [
            { id: 1, name: "豪华旅行套装", description: "含高级行李箱、旅行收纳包等豪华旅行装备", image: "https://images.unsplash.com/photo-1581553680321-4fffae59fccd?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 2, name: "高级电子产品", description: "最新款智能手机或平板电脑", image: "https://images.unsplash.com/photo-1510878933023-e2e2e3942fb0?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 3, name: "限量版手办", description: "珍贵的限量版动漫人物手办", image: "https://images.unsplash.com/photo-1513384312027-9fa69a360337?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 4, name: "精美礼品卡", description: "知名品牌购物礼品卡", image: "https://images.unsplash.com/photo-1561715276-a2d087060f1d?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 5, name: "神秘珍藏品", description: "独特的收藏级别礼品", image: "https://images.unsplash.com/photo-1599707367072-cd6ada2bc375?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 6, name: "幸运现金红包", description: "随机金额的现金奖励", image: "https://images.unsplash.com/photo-1640244674671-5c70b6e6b345?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 7, name: "豪华游戏主机", description: "最新款游戏主机套装", image: "https://images.unsplash.com/photo-1486401899868-0e435ed85128?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 8, name: "智能家居套装", description: "智能音箱、智能灯具等家居套装", image: "https://images.unsplash.com/photo-1556761175-5973dc0f32e7?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" },
            { id: 9, name: "奢华美食礼盒", description: "精选美食组合礼盒", image: "https://images.unsplash.com/photo-1607920592519-bab2a80f2c74?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600" }
        ];
        return gameState.prizes;
    }
    
    // 创建盲盒元素
    function createBoxes(prizes) {
        mysteryBoxesContainer.innerHTML = ''; // 清空容器
        gameState.boxes = [];
        
        prizes.forEach((prize, index) => {
            const box = document.createElement('div');
            box.className = 'box';
            box.dataset.prizeId = prize.id;
            box.dataset.prizeName = prize.name;
            box.dataset.prizeDescription = prize.description;
            
            const boxFront = document.createElement('div');
            boxFront.className = 'box-front';
            boxFront.textContent = '?';
            
            const boxBack = document.createElement('div');
            boxBack.className = 'box-back';
            
            box.appendChild(boxFront);
            box.appendChild(boxBack);
            
            // 添加点击事件
            box.addEventListener('click', () => handleBoxClick(box, index));
            
            mysteryBoxesContainer.appendChild(box);
            gameState.boxes.push(box);
        });
    }
    
    // 处理盲盒点击事件
    function handleBoxClick(box, index) {
        // 如果盲盒已经打开或已禁用，则不执行操作
        if (box.classList.contains('flipped') || box.classList.contains('disabled')) {
            return;
        }
        
        // 检查是否还有剩余次数
        if (gameState.remainingChances <= 0) {
            showNoChancesMessage();
            return;
        }
        
        // 播放点击音效
        playClickSound();
        
        // 添加翻转动画
        box.classList.add('flipped');
        
        // 记录已打开的盲盒
        gameState.openedBoxes.push(index);
        
        // 减少剩余次数
        gameState.remainingChances--;
        updateRemainingChances();
        
        // 获取奖品信息
        const prizeId = parseInt(box.dataset.prizeId);
        const prizeName = box.dataset.prizeName;
        const prizeDescription = box.dataset.prizeDescription;
        const prize = gameState.prizes.find(p => p.id === prizeId);
        const prizeImage = prize ? prize.image : 'images/default-prize.jpg';
        
        // 设置盲盒背面内容
        const boxBack = box.querySelector('.box-back');
        boxBack.textContent = prizeName;
        
        // 添加盒子打开的动画效果
        addOpeningEffect(box);
        
        // 延迟显示奖品弹窗，让用户先看到盲盒翻转
        setTimeout(() => {
            showPrizeModal(prizeName, prizeDescription, prizeImage);
        }, 1000);
    }
    
    // 更新剩余次数显示
    function updateRemainingChances() {
        remainingChancesElement.textContent = gameState.remainingChances;
        
        // 如果没有剩余次数，禁用所有未开启的盲盒
        if (gameState.remainingChances === 0) {
            disableUnopendBoxes();
        }
    }

    // 禁用未开启的盲盒
    function disableUnopendBoxes() {
        gameState.boxes.forEach((box, index) => {
            if (!gameState.openedBoxes.includes(index)) {
                box.classList.add('disabled');
            }
        });
    }

    // 重置游戏状态
    function resetGame() {
        gameState.remainingChances = 3;
        gameState.openedBoxes = [];
        
        // 重置所有盲盒
        gameState.boxes.forEach(box => {
            box.classList.remove('flipped', 'disabled');
            const boxBack = box.querySelector('.box-back');
            boxBack.textContent = '';
        });
        
        // 重置奖品图片 - 使用默认图片而不是空字符串
        const prizeImageElement = document.querySelector('.prize-image');
        // 不设置空字符串，避免触发错误
        prizeImageElement.style.display = 'none';
        prizeImageElement.alt = '';
        
        // 更新显示
        updateRemainingChances();
        
        // 添加初始动画
        addInitialAnimations();
    }

    // 显示奖品弹窗
    function showPrizeModal(prizeName, prizeDescription, prizeImage) {
        const prizeNameElement = document.querySelector('.prize-name');
        const prizeImageElement = document.querySelector('.prize-image');
        
        prizeNameElement.innerHTML = `
            <div class="prize-title">${prizeName}</div>
            <div class="prize-desc">${prizeDescription}</div>
        `;
        
        // 加载奖品图片
        const prizeImgElement = document.querySelector('.prize-image');
        prizeImgElement.src = '';
        prizeImgElement.alt = prizeName;
        prizeImgElement.className = 'prize-image loading-placeholder';
        
        // 显示图片元素
        prizeImgElement.style.display = 'block';
        
        // 设置加载超时检查
        const loadTimeout = setTimeout(() => {
            if (!prizeImgElement.classList.contains('loaded')) {
                // 使用可靠的默认图片
                prizeImgElement.src = 'https://images.unsplash.com/photo-1513542789411-b6a5d4f31634?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600';
                prizeImgElement.className = 'prize-image loaded';
            }
        }, 3000);
        
        prizeImgElement.onload = function() {
            clearTimeout(loadTimeout);
            this.className = 'prize-image loaded';
            this.style.display = 'block';
        };
        
        prizeImgElement.onerror = function() {
            clearTimeout(loadTimeout);
            console.error('图片加载失败:', prizeImage);
            // 使用可靠的默认图片
            this.src = 'https://images.unsplash.com/photo-1513542789411-b6a5d4f31634?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&h=600';
            this.className = 'prize-image loaded';
            this.style.display = 'block';
        };
        
        // 开始加载实际图片
        prizeImgElement.src = prizeImage;
        
        modal.classList.add('show');
        
        // 添加奖品展示的动画效果
        addPrizeRevealEffect();
    }

    // 显示没有剩余次数的提示
    function showNoChancesMessage() {
        const prizeNameElement = document.querySelector('.prize-name');
        prizeNameElement.innerHTML = `
            <div class="prize-title">抽取次数已用完！</div>
            <div class="prize-desc">每次游戏最多只能抽取3个盲盒，请点击"重新开始"按钮重置游戏。</div>
        `;
        modal.classList.add('show');
    }
    
    // 关闭弹窗事件
    closeModalBtn.addEventListener('click', () => {
        hideModal();
    });
    
    // 点击弹窗外部也可关闭
    modal.addEventListener('click', (e) => {
        if (e.target === modal) {
            hideModal();
        }
    });
    
    // 重置游戏按钮事件
    resetGameButton.addEventListener('click', resetGame);
    
    // 隐藏弹窗
    function hideModal() {
        modal.classList.remove('show');
    }
    
    // 播放点击音效
    function playClickSound() {
        // 这里可以添加音效播放代码
        // 例如：const audio = new Audio('click.mp3'); audio.play();
        // 由于我们没有音效文件，这里只是预留功能
    }
    
    // 添加盒子打开的动画效果
    function addOpeningEffect(box) {
        // 添加闪光效果
        const flash = document.createElement('div');
        flash.className = 'box-flash';
        flash.style.cssText = `
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 15px;
            background: white;
            opacity: 0;
            z-index: 2;
            pointer-events: none;
        `;
        box.appendChild(flash);
        
        // 闪光动画
        setTimeout(() => {
            flash.style.opacity = '0.8';
            setTimeout(() => {
                flash.style.opacity = '0';
                setTimeout(() => {
                    box.removeChild(flash);
                }, 300);
            }, 100);
        }, 50);
    }
    
    // 添加奖品展示的动画效果
    function addPrizeRevealEffect() {
        const prizeNameElement = document.querySelector('.prize-name');
        prizeNameElement.style.transform = 'scale(0.5)';
        prizeNameElement.style.opacity = '0';
        
        setTimeout(() => {
            prizeNameElement.style.transition = 'all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275)';
            prizeNameElement.style.transform = 'scale(1)';
            prizeNameElement.style.opacity = '1';
        }, 100);
    }
    
    // 添加初始动画效果
    function addInitialAnimations() {
        gameState.boxes.forEach((box, index) => {
            box.style.opacity = '0';
            box.style.transform = 'translateY(50px)';
            
            setTimeout(() => {
                box.style.transition = 'all 0.5s ease';
                box.style.opacity = '1';
                box.style.transform = 'translateY(0)';
            }, 100 + index * 100);
        });
    }
    
    // 初始化游戏
    function initGame() {
        // 初始化奖品数据
        const prizes = initPrizes();
        
        // 创建盲盒元素
        createBoxes(prizes);
        
        // 更新剩余次数显示
        updateRemainingChances();
        
        // 添加初始动画
        addInitialAnimations();
        
        console.log('游戏初始化完成，已加载', prizes.length, '个奖品');
    }
    
    // 页面加载时初始化游戏
    initGame();
});